import wepy from 'wepy'
import {
    post,
    get
} from '../utils/request';
import * as OauthApi from './OauthApi';
import storage from '../utils/storage';




/**
 * @description 发送短信验证码
 * @param {String} phone 手机号
 */
export async function sendCode(phone) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 2) {
            times = arguments[2];
        }
        let result = await OauthApi.postWithClient(config.BASE_URL + "/apis/show/code-anon/getcode", {
            phone: phone,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return sendCode(phone, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetCredentialsToken();
                    return sendCode(phone, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}

/**
 * @description 绑定手机号
 * @param {Number} phone 手机号
 * @param {Number} verfiyCode 验证码
 * @param {String} username  用户名
 * @param {String} shopId 店铺id 必传
 * @param {String} openid 用户openid 必传
 * @param {String} share_openid 推荐人openid
 */
export async function bingdingPhone(phone, verfiyCode, username, openid, shopId, share_openid) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 6) {
            times = arguments[6];
        }
        let result = await OauthApi.postWithClient(config.BASE_URL + "/show/code-anon/register", {
            phone: phone,
            verfiyCode: verfiyCode,
            username: username,
            shopId: shopId,
            openid: openid,
            share_openid: share_openid
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return bingdingPhone(phone, verfiyCode, username, openid, shopId, share_openid, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return bingdingPhone(phone, verfiyCode, username, openid, shopId, share_openid, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}   




/**
 * @description 登录
 * @param {string}  grant_type | 是 | string | 授权类型 (固定为:password)
 * 
 */
export async function long(username, password) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.postWithoutToken(config.BASE_URL + "/code-anon/register", {
            grant_type: 'password',
            client_id: '100008',
            client_secret: config.CLIENT_SECRECT,
            username: username,
            password: password,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return long(grant_type, client_id, client_secret, username, password, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return long(grant_type, client_id, client_secret, username, password, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}


/**
 * @description 首页接口
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)                          |
 */
export async function index(access_token, type) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.getWithToken(config.BASE_URL + "/api/v1/index/", {
            access_token: access_token,
            type: type
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return index(access_token, type, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return index(access_token, type, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}


/**
 * @description 我的接口
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)|
 */
export async function mine() {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.getWithToken(config.BASE_URL + "/api/v1/index/mine", {
            access_token: config.access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return mine(++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return mine(++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}

/**
 * @description 获取医院同事
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)|
 */
export async function list(access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.postWithoutToken(config.BASE_URL + "/api/v1/partner/list", {
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return list(++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return list(++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}

/**
 * @description 获取同事详情
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)|
 */
export async function detail(employee_id, access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.postWithoutToken(config.BASE_URL + "/api/v1/partner/detail", {
            employee_id: employee_id,
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return detail(employee_id, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return detail(employee_id, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}


/**
 * @description 获取今天工作
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password) |
 */
/**
 * 
 */
export async function today(access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.postWithoutToken(config.BASE_URL + "/api/v1/order/today", {
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return today(++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return today(++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}


/**
 * @description 获取所有医生
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)                          |
 */
export async function docter(access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.postWithoutToken(config.BASE_URL + "/api/v1/order/docter", {
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return docter(++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return docter(++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}



/**
 * @description 预约列表
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)                                 |
| date | 否 | string | 查询时间 |
| page | 是 | int | 页码 |
| pageSize | 是 | int | 页数 |
| employee_id | 否 | int | 医生序号 |                         |
 */
export async function order(page, pageSize, date, employee_id, access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.postWithoutToken(config.BASE_URL + "/api/v1/order/order", {
            page: page,
            pageSize: pageSize,
            date: date,
            employee_id: employee_id,
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return order(page, pageSize, date, employee_id, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return order(page, pageSize, date, employee_id, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}


/**
 * @description 历史欠款
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)                      
| page | 是 | int | 页码 
| pageSize | 是 | int | 页数 
| employee_id | 否 | int | 医生序号                    
 */
export async function arrear(page, pageSize, employee_id, access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.postWithoutToken(config.BASE_URL + "/api/v1/order/arrear", {
            page: page,
            pageSize: pageSize,
            employee_id: employee_id,
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return arrear(employee_id, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return arrear(employee_id, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}


/**
 * @description 业绩明细
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)                      
| type | 否 | string | 类型 YEAR-按年 MONTH-按月 DAY-按日 |
| year | 否 | string | 年份 |
| month | 否 | string | 月份 |
| day | 否 | string | 日期 |                   
 */
export async function statistics(type, year, month, day, access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.postWithoutToken(config.BASE_URL + "/api/v1/statistics/list", {
            type: type,
            year: year,
            month: month,
            day: day,
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return statistics(type, year, month, day, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return statistics(type, year, month, day, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}


/**
 * @description 病历信息
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)                      
| case_number | 是 | string | 病历号 |
| page | 是 | string | 页码 |
| pageSize | 是 | string | 页数 |                  
 */
export async function ordercase(case_number, page, pageSize, access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.getWithToken(config.BASE_URL + "/api/v1/order/case", {
            case_number: case_number,
            page: page,
            pageSize: pageSize,
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return ordercase(case_number, page, pageSize, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return ordercase(case_number, page, pageSize, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}


/**
 * @description 获取患者
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)                      
 * @param time_type
 * @param level_id
 * @param source          
 */
export async function patient(time_type, level_id, source, access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.getWithToken(config.BASE_URL + "/api/v1/order/patient", {
            time_type: time_type,
            level_id: level_id,
            source: source,
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return patient(time_type, level_id, source, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return patient(time_type, level_id, source, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}

/**
 * @description 获取等级
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)                               
 */
export async function level(access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.getWithToken(config.BASE_URL + "/api/v1/order/level", {
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return level(++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return level(++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}


/**
 * @description 获取诊所
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)                               
 */
export async function hospital(access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.getWithToken(config.BASE_URL + "/api/v1/index/hospital", {
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return hospital(++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return hospital(++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}





/**
 * @description 修改密码
 * @param {string}  access_token | 是 | string | 授权类型 (固定为:password)                      
| old_password | 是 | string | 旧密码 |
| new_password | 是 | string | 新密码 |                
 */
export async function password(old_password, new_password, access_token) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        // postWithToken  postWithoutToken
        let result = await OauthApi.getWithToken(config.BASE_URL + "/api/v1/index/reset", {
            old_password: old_password,
            new_password: new_password,
            access_token: access_token,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return password(old_password, new_password, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return password(old_password, new_password, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }
        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}

























